using System;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;

namespace TCPSocketClient
{
	/// <summary>
	/// Summary description for Client.
	/// </summary>
	public class Client : Form
	{
		/////////////////////////////////////////////////////////////////////////////
		///Variables & Properties
		/////////////////////////////////////////////////////////////////////////////
		private Button btnConnectToServer;
		private Button btnSendMessage;
		private StreamReader clientStreamReader;
		private StreamWriter clientStreamWriter;
		
		/////////////////////////////////////////////////////////////////////////////
		///Constructor
		public Client()
		{
			//create ConnectToServer button, set its properties & event handlers
			this.btnConnectToServer = new Button();
			this.btnConnectToServer.Text = "Connect";
			this.btnConnectToServer.Click += new System.EventHandler(btnConnectToServer_Click);			
			//create SendMessage button, set its properties & event handlers
			this.btnSendMessage = new Button();
			this.btnSendMessage.Text = "Send Message";
			this.btnSendMessage.Top += 30;
			this.btnSendMessage.Width += 20;
			this.btnSendMessage.Click += new System.EventHandler(btnSendMessage_Click);				

			//add controls to windows form
			this.Controls.Add(this.btnConnectToServer);
			this.Controls.Add(this.btnSendMessage);
		}

		/////////////////////////////////////////////////////////////////////////////
		///Main method
		public static void Main(string[] args)
		{
			//create n display windows form
			Client tcpSockClient = new Client();
			Application.Run(tcpSockClient);
		}

		/////////////////////////////////////////////////////////////////////////////
		///Connect to server
		private bool ConnectToServer()
		{
			//connect to server at given port
			try
			{
                TcpClient tcpClient = new TcpClient("54.191.140.228", 4444); 
				Console.WriteLine("Connected to Server");
				//get a network stream from server
				NetworkStream clientSockStream = tcpClient.GetStream();
				clientStreamReader = new StreamReader(clientSockStream);
				clientStreamWriter = new StreamWriter(clientSockStream);
			}
			catch(Exception e)
			{
				Console.WriteLine(e.StackTrace);
				return false;
			}

			return true;
		}

		/////////////////////////////////////////////////////////////////////////////
		///Event Handlers
		/////////////////////////////////////////////////////////////////////////////
		private void btnConnectToServer_Click(object sender,System.EventArgs e)
		{
			//connect to server
			if (!ConnectToServer())
				Console.WriteLine("Unable to connect to server");
		}

		private void btnSendMessage_Click(object sender,System.EventArgs e)
		{
			try
			{
				//send message to server
				clientStreamWriter.WriteLine("Hello!");
				clientStreamWriter.Flush();
				Console.WriteLine("SERVER: "+clientStreamReader.ReadLine());
			}
			catch(Exception se)
			{
				Console.WriteLine(se.StackTrace);
			}
		}
	}
}
